package by.grsu.shakh.page.librarian;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.shakh.app.LibraryWebApplication;
import by.grsu.shakh.dao.BookDao;
import by.grsu.shakh.dao.BookItemDao;
import by.grsu.shakh.entity.Book;
import by.grsu.shakh.entity.BookItem;

/**
 * list page for Book object.
 * 
 * @author shakh_andrew
 */
public class ListBookPage extends WebPage {

	public ListBookPage() {
		super();

		final BookDao bookDao = new BookDao(
				LibraryWebApplication.DATA_XML_FOLDER);
		final List<Book> allBooks = bookDao.getAll();

		add(new DataView<Book>("table-rows", new ListDataProvider<Book>(
				allBooks)) {
			@Override
			protected void populateItem(final Item<Book> item) {
				final Book book = item.getModelObject();

				item.add(new Label("id", book.getId()));
				item.add(new Label("name", book.getName()));
				item.add(new Label("catalogid", book.getCatalogId()));
				item.add(new Label("author", book.getAuthor()));
				item.add(new Label("date", book.getDate()));
				item.add(new Label("publishing", book.getPublishing()));
				item.add(new Label("description", book.getDescription()));

				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new BookEditPage(book));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						bookDao.delete(book.getId());
						BookItemDao bookItemDao = new BookItemDao(
								LibraryWebApplication.DATA_XML_FOLDER);
						List<BookItem> listBookItem = bookItemDao.getAll();
						for (BookItem b : listBookItem) {
							if (b.getBookid().equals(book.getId()))
								bookItemDao.delete(b.getId());
						}
						setResponsePage(new ListBookPage());
					}
				});
				item.add(new Link("deleteCopy") {
					@Override
					public void onClick() {
						BookItemDao bookItemDao = new BookItemDao(
								LibraryWebApplication.DATA_XML_FOLDER);
						List<BookItem> listBookItem = bookItemDao.getAll();
						for (BookItem b : listBookItem) {
							if (b.getBookid().equals(book.getId()))
								if (b.getOwnerId() == null) {
									bookItemDao.delete(b.getId());
									break;
								}
						}
					}
				});
				item.add(new Link("addCopy") {
					@Override
					public void onClick() {
						BookItemDao bookItemDao = new BookItemDao(
								LibraryWebApplication.DATA_XML_FOLDER);
						BookItem newBook = new BookItem();
						newBook.setBookid(book.getId());
						bookItemDao.saveNew(newBook);
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new BookEditPage(new Book()));
			}
		});
	}
}
